package socket;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2024/09/25
 */
public class URLTest {
    @Test
    public void URLTEst() throws IOException {
        URL qqUrl = new URL("https://dldir1.qq.com/qqfile/qq/QQNT/Windows/QQ_9.9.15_240925_x64_01.exe");
        System.out.println(qqUrl.getPath());
        System.out.println("qqUrl.getFile() = " + qqUrl.getFile());

        //链接URL
        URLConnection urlConnection = qqUrl.openConnection();
        //按照输出流的方式下载
        InputStream inputStream = urlConnection.getInputStream();
        OutputStream outputStream = new FileOutputStream("D:\\1java测试\\QQ.exe");//小错误：原先写法:F:\1java测试,要有文件名
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
    }

    /**
     * @author 杜伟毅
     * @since 2024/9/26 0026 10:22
     * 先运行这个服务端
     */
    @Test
    public void serverTest() throws IOException {
        //创建ServerSocket
        ServerSocket serverSocket = new ServerSocket();
        //绑定在某一个端口上
        serverSocket.bind(new InetSocketAddress(8888));
        //监听在这个端口(阻塞的)
        Socket socket = serverSocket.accept();

        //IO流
        InputStream inputStream = socket.getInputStream();
        //读流
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, len));
        }

        inputStream.close();
        socket.close();
    }

    /**
     * @author 杜伟毅
     * @since 2024/9/26 0026 10:14
     * 客户端
     */
    @Test
    public void clientTest() throws IOException {
        //创建Socket(动态的)
        Socket socket = new Socket();
        //使用这个socket进行连接
        socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), 8888));

        //IO流
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        //向客户端发送消息
        outputStream.write("hello".getBytes());

        outputStream.close();
        inputStream.close();
        socket.close();
    }

    /**
     * UDP链接(客户端)
     *
     * @author 杜伟毅
     * @since 2024/9/26 0026 10:28
     */
    @Test
    public void udpClientTest() throws IOException {
        //创建Socket(动态的)
        DatagramSocket datagramSocket = new DatagramSocket(7001);

        String data = "hello udp";
        //创建数据包
        DatagramPacket packet = new DatagramPacket(
                data.getBytes(), 0, data.getBytes().length, new InetSocketAddress(InetAddress.getByName("localhost"), 8080)
        );
        //发送数据包
        datagramSocket.send(packet);
        datagramSocket.close();

    }

    /**
     * UDP链接（服务端）
     *
     * @author 杜伟毅
     * @since 2024/9/26 0026 10:28
     */
    @Test
    public void udpServerTest() throws IOException {
        //创建Socket(动态的)
        DatagramSocket datagramSocket = new DatagramSocket(8080);
        //建立一个接收包
        byte[] buffer = new byte[1024];
        //创建数据包
        DatagramPacket packet = new DatagramPacket(
                buffer, 0, buffer.length
        );
        //接收包
        datagramSocket.receive(packet);
        System.out.println(new String(buffer,0,packet.getLength()));
        datagramSocket.close();
    }
}
